Lua开源库 lfs的介绍与使用 您所在的位置:网站首页 lua require的用法 Lua开源库 lfs的介绍与使用

Lua开源库 lfs的介绍与使用

#Lua开源库 lfs的介绍与使用| 来源: 网络整理| 查看: 265

Lua lfs库

这个库可以实现平台无关(Linux和Windows通用)的文件系统访问 官网地址 Github项目地址

如何配置:

5.1以上的lua已经包含了lfs库,路径是Lua5.1\clibs\lfs.dll,我们无需配置直接require “lfs”使用即可。

提供的功能:

lfs的开发提供了功能的介绍:官方手册

下面给出精简了内容的中文版(方便不喜欢看英文版的同学): - lfs.attributes (filepath [, aname]) 返回这个path的属性table,如果filepath是nil则会出错并打印错误信息,属性列表如下所示: mode属性是字符串,其他属性都是数组。

属性描述dev不常用不翻译了:on Unix systems, this represents the device that the inode resides on. On Windows systems, represents the drive number of the disk containing the fileinoUnix系统下表示inode数目,Windows系统下无意义mode这个字符串表示关联的保护模式,值可能是file、directory、link、socket、named pipe、char device、block device or othernlink文件上的硬链接数uid目录的user-id(Unix only, always 0 on Windows)gid用户的group-id(Unix only, always 0 on Windows)rdevlinux系统下rdev表示设备类型,Windows系统下和dev值相同access最近一次访问时间modification最近一次修改时间change最近一次文件状态修改时间size文件大小(以字节为单位)blocks分配给文件的block(Unix Only)blksize不常用不翻译了:optimal file system I/O blocksize; (Unix only)

- lfs.chdir (path) 将当前目录改为给定的path - lfs.currentdir () 获取当前目录 - lfs.dir (path) Lua遍历目录下的所有入口,每次迭代都返回值为入口名的字符串 - lfs.lock (filehandle, mode[, start[, length]]) 锁定一个文件或这文件的部分内容 - lfs.mkdir (dirname) 创建一个目录 - lfs.rmdir (dirname) 移除一个已存在的目录 - lfs.setmode (file, mode) 设置文件的写入模式,mode字符串可以是binary或text - lfs.symlinkattributes (filepath [, aname]) 比lfs.attributes多了the link itself (not the file it refers to)信息,其他都和lfs.attribute一样 - lfs.touch (filepath [, atime [, mtime]]) 设置上一次使用和修改文件的时间值 - lfs.unlock (filehandle[, start[, length]]) 解锁文件或解锁文件的部分内容

小实例 传入一个根目录路径,递归获取该路径子目录的所有文件全路径: local lfs = require "lfs" local allFilePath = {} function main() -- 打印lfs库的版本 PrintLfsVersion() -- 打印当前目录地址 PrintCurrrentDir() -- 打印当前目录的下一层子目录地址 local rootPath = lfs.currentdir() PrintDirChildren(rootPath) -- 递归打印当前目录下面所有层级的子目录并将文件地址保存到表中 GetAllFiles(rootPath) PrintTable(allFilePath) end function PrintLfsVersion( ... ) print(lfs._VERSION) end function PrintCurrrentDir( ... ) local rootPath = lfs.currentdir() print(rootPath) end function PrintDirChildren(rootPath) for entry in lfs.dir(rootPath) do if entry~='.' and entry~='..' then local path = rootPath.."\\"..entry local attr = lfs.attributes(path) assert(type(attr)=="table") --如果获取不到属性表则报错 -- PrintTable(attr) if(attr.mode == "directory") then print("Dir:",path) elseif attr.mode=="file" then print("File:",path) end end end end function GetAllFiles(rootPath) for entry in lfs.dir(rootPath) do if entry~='.' and entry~='..' then local path = rootPath.."\\"..entry local attr = lfs.attributes(path) assert(type(attr)=="table") --如果获取不到属性表则报错 -- PrintTable(attr) if(attr.mode == "directory") then -- print("Dir:",path) GetAllFiles(path) --自调用遍历子目录 elseif attr.mode=="file" then -- print(attr.mode,path) table.insert(allFilePath,path) end end end end function PrintTable( tbl , level, filteDefault) local msg = "" filteDefault = filteDefault or true --默认过滤关键字(DeleteMe, _class_type) level = level or 1 local indent_str = "" for i = 1, level do indent_str = indent_str.." " end print(indent_str .. "{") for k,v in pairs(tbl) do if filteDefault then if k ~= "_class_type" and k ~= "DeleteMe" then local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v)) print(item_str) if type(v) == "table" then PrintTable(v, level + 1) end end else local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v)) print(item_str) if type(v) == "table" then PrintTable(v, level + 1) end end end print(indent_str .. "}") end main() 参考:

lua使用lfs.dll库进行文件操作



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有